#include "StdAfx.h"
#include "LinkED_DataQuene.h"
#include "LinkED_PCDlg.h"

CDataQuene* CDataQuene::m_pInstance = NULL;

CDataQuene::CDataQuene(void)
    :m_MouseMsgNumber(0)
    ,m_pMainPage(NULL)
{

}


CDataQuene::~CDataQuene(void)
{

}

bool CDataQuene::CreateInstance(CMainDlg* mainpage)
{
    if (m_pInstance != NULL)
    {
        return true;
    }

    m_pInstance = new(std::nothrow) CDataQuene;

    if (NULL == m_pInstance)
    {
        return false;
    }

    m_pInstance->m_pMainPage = mainpage;

    return true;

}

CDataQuene* CDataQuene::GetInstance()
{
    
    return m_pInstance;
}

void CDataQuene::DeleteInstance()
{
    if (m_pInstance != NULL)
    {
        delete m_pInstance;
        m_pInstance = NULL;
    }
}

void CDataQuene::PushMouseMoveInput(const LPScreenPos pos)
{
    m_MouseMsgNumber++;

    INPUT InputMouseMove[2];
    ZeroMemory(InputMouseMove, sizeof(INPUT)*2);

    InputMouseMove[0].type = INPUT_MOUSE;
    InputMouseMove[1].type = INPUT_MOUSE;
    InputMouseMove[0].mi.dx = pos->x;
    InputMouseMove[0].mi.dy = pos->y;
    InputMouseMove[1].mi.dx = pos->x;
    InputMouseMove[1].mi.dy = pos->y;
    InputMouseMove[0].mi.dwExtraInfo = NULL;
    InputMouseMove[1].mi.dwExtraInfo = NULL;

    //m_pMainPage->m_NotifyInfo.Format("X: %d, Y: %d, Type: %d, ID: %d", 
    //    pos->x, pos->y, pos->msg_type, m_MouseMsgNumber);

    //::PostMessage(m_pMainPage->GetSafeHwnd(), LinkED_MSG_UPDATE_MAIN_PAGE, 0, 0);

    switch(pos->msg_type)
    {
    case LBTN_CLICKED:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
        InputMouseMove[0].mi.time = 0;
        SendInput(1, InputMouseMove, sizeof(INPUT));
        break;
    case LBTN_DOUBLE_CLICKED:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
        InputMouseMove[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
        InputMouseMove[0].mi.time = 0;
        InputMouseMove[1].mi.time = 0;
        SendInput(2, InputMouseMove, sizeof(INPUT));
        break;
    case RBTN_CLICKED:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP;
        InputMouseMove[0].mi.time = 0;
        SendInput(1, InputMouseMove, sizeof(INPUT));
        break;
    case MOUSE_MOVE:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_MOVE;
        InputMouseMove[0].mi.time = 0;
        SendInput(1, InputMouseMove, sizeof(INPUT));
        break;
    case CENTER_SCROLL:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_WHEEL;
        InputMouseMove[0].mi.mouseData = (DWORD)(pos->x + pos->y);
        InputMouseMove[0].mi.time = 0;
        SendInput(1, InputMouseMove, sizeof(INPUT));
        break;
    case LEFT_DRAG:
        InputMouseMove[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN;
        InputMouseMove[0].mi.time = 0;
        SendInput(1, InputMouseMove, sizeof(INPUT));
        break;
    case SHOW_DESKTOP:
        {
            CoInitialize(0);
            IShellDispatch4 * pdisp=NULL;
            CoCreateInstance(CLSID_Shell,NULL,CLSCTX_ALL,__uuidof(IShellDispatch4),(void **)&pdisp);
            pdisp->ToggleDesktop();
            pdisp->Release();
            CoUninitialize();
        }
        break;
    case OPEN_COMPUTER:
        OpenMyComputer();
        break;
    case OPEN_START_MENU:
        OpenStartMenu();
        break;
    case OPEN_IE_BROWSER:
        OpenIEBrowser();
        break;
    case OPEN_CACULATOR:
        OpenCaculator();
        break;
    case OPEN_MUSIC_PALYER:
        OpenMusicPlayer();
        break;
    case OPEN_CONTROL_PANEL:
        OpenControlPanel();
        break;
    case OPEN_TECENT_QQ:
        OpenTecentQQ();
        break;
    case OPEN_RECYCLE_BIN:
        OpenRecycleBin();
        break;
    case OPEN_MY_DOCS:
        OpenMyDocs();
        break;
    }
}

void CDataQuene::OpenMyComputer()
{
    LPITEMIDLIST pidl; 
    LPMALLOC pMalloc; 

    if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidl)))
    { 
        SHELLEXECUTEINFO sei; 
        ZeroMemory(&sei, sizeof(sei)); 
        sei.cbSize = sizeof(sei); 
        sei.fMask = SEE_MASK_IDLIST; 
        sei.lpIDList = pidl; 
        sei.lpVerb = "open"; 
        sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
        sei.nShow = SW_SHOWNORMAL; 
        ShellExecuteEx(&sei); 

        if (SUCCEEDED(SHGetMalloc(&pMalloc)))
        { 
            pMalloc->Free (pidl); 
            pMalloc->Release(); 
        }
    }
}

void CDataQuene::OpenStartMenu()
{
    OSVERSIONINFO osv;
    osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    if (GetVersionEx(&osv))
    {
        // for xp os
        if (osv.dwMajorVersion <6)
        {
            HWND h1 = FindWindow("Shell_TrayWnd", NULL);

            if(h1)
            {
                HWND h2 = FindWindowEx(h1, 0, "Button", NULL);

                if(h2)
                {
                    ::PostMessage(h2, WM_LBUTTONDOWN, 0, 0);
                }
            }
        }
        else// for win7 and higher os
        {
            HWND h1 = FindWindow("Button", NULL);

            if (h1)
            {
                ::PostMessage(h1, WM_LBUTTONDOWN, 0, 0);
            }
        }
    }
    else
    {
        HWND h1 = FindWindow("Shell_TrayWnd", NULL);

        if(h1)
        {
            HWND h2 = FindWindowEx(h1, 0, "Button", NULL);

            if(h2)
            {
                ::PostMessage(h2, WM_LBUTTONDOWN, 0, 0);
            }
        }
    }

}

void CDataQuene::OpenIEBrowser()
{
    WinExec("C:\\Program Files\\Internet Explorer\\iexplore.exe", SW_SHOWNORMAL);
}

void CDataQuene::OpenMusicPlayer()
{
    WinExec("C:\\Program Files\\Windows Media Player\\wmplayer.exe", SW_SHOWNORMAL);
}

void CDataQuene::OpenCaculator()
{
    WinExec("C:\\Windows\\System32\\calc.exe", SW_SHOWNORMAL);
}

void CDataQuene::OpenControlPanel()
{
    LPITEMIDLIST pidl; 
    LPMALLOC pMalloc; 

    if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_CONTROLS, &pidl)))
    { 
        SHELLEXECUTEINFO sei; 
        ZeroMemory(&sei, sizeof(sei)); 
        sei.cbSize = sizeof(sei); 
        sei.fMask = SEE_MASK_IDLIST; 
        sei.lpIDList = pidl; 
        sei.lpVerb = "open"; 
        sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
        sei.nShow = SW_SHOWNORMAL; 
        ShellExecuteEx(&sei); 

        if (SUCCEEDED(SHGetMalloc(&pMalloc)))
        { 
            pMalloc->Free (pidl); 
            pMalloc->Release(); 
        }
    }
}

void CDataQuene::OpenTecentQQ()
{
    WinExec("C:\\Program Files\\Tencent\\QQ\\Bin\\QQ.exe", SW_SHOWNORMAL);
}

void CDataQuene::OpenMyDocs()
{
    LPITEMIDLIST pidl; 
    LPMALLOC pMalloc; 

    if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl)))
    { 
        SHELLEXECUTEINFO sei; 
        ZeroMemory(&sei, sizeof(sei)); 
        sei.cbSize = sizeof(sei); 
        sei.fMask = SEE_MASK_IDLIST; 
        sei.lpIDList = pidl; 
        sei.lpVerb = "open"; 
        sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
        sei.nShow = SW_SHOWNORMAL; 
        ShellExecuteEx(&sei); 

        if (SUCCEEDED(SHGetMalloc(&pMalloc)))
        { 
            pMalloc->Free (pidl); 
            pMalloc->Release(); 
        }
    }
}

void CDataQuene::OpenRecycleBin()
{
    LPITEMIDLIST pidl; 
    LPMALLOC pMalloc; 

    if (SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_BITBUCKET, &pidl)))
    { 
        SHELLEXECUTEINFO sei; 
        ZeroMemory(&sei, sizeof(sei)); 
        sei.cbSize = sizeof(sei); 
        sei.fMask = SEE_MASK_IDLIST; 
        sei.lpIDList = pidl; 
        sei.lpVerb = "open"; 
        sei.hwnd = AfxGetMainWnd()->GetSafeHwnd();
        sei.nShow = SW_SHOWNORMAL; 
        ShellExecuteEx(&sei); 

        if (SUCCEEDED(SHGetMalloc(&pMalloc)))
        { 
            pMalloc->Free (pidl); 
            pMalloc->Release(); 
        }
    }
}

